package com.atguigu.srb.core.controller.api;


import com.alibaba.fastjson.JSON;
import com.atguigu.srb.base.util.JwtUtils;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.util.Assert;
import com.atguigu.srb.core.hfb.RequestHelper;
import com.atguigu.srb.core.pojo.entity.UserBind;
import com.atguigu.srb.core.pojo.vo.UserBindVO;
import com.atguigu.srb.core.service.UserBindService;
import com.atguigu.srb.core.service.UserInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 用户绑定表 前端控制器
 * </p>
 *
 * @author fan
 * @since 2021-11-22
 */
@RestController
@RequestMapping("/api/core/userBind")
public class ApiUserBindController {
    @Resource
    private UserInfoService userInfoService;
    @Resource
    private UserBindService userBindService;
    @PostMapping("/auth/bind")
    public R userBind(@RequestBody UserBindVO userBindVO, HttpServletRequest request){
        String token = request.getHeader("token");

        if(!StringUtils.isEmpty(token)){
            String userId = JwtUtils.getUserId(token);
            String formStr = userBindService.bind(userBindVO,userId);//调用汇付宝openApi生成表单
            return R.ok().data("formStr",formStr);
        }else {
            return R.error();
        }
    }
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){

        Map<String, String[]> parameterMap = request.getParameterMap();
        Map<String, Object> stringObjectMap = RequestHelper.switchMap(parameterMap);

       //校验签名

        userBindService.chanNotify(parameterMap,stringObjectMap);
        // 调用业务层，修改userBind，userInfo
        //agentUserId
//        userInfoService.updateUserInfo(stringObjectMap);

        return "success";
    }
}

